package indi.zhifa.engine.redis.properties;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.util.StringUtils;

public class RedisConfig {
    public static RedisConnectionFactory redisConnectionFactory(RedisProperties pRedisProperties){
        RedisConnectionFactory redisConnectionFactory = null;
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(pRedisProperties.getHost(), pRedisProperties.getPort());
        if(StringUtils.hasText(pRedisProperties.getPassword())){
            redisConfig.setPassword(pRedisProperties.getPassword());
        }
        if(pRedisProperties.getDatabase() >=0){
            redisConfig.setDatabase(pRedisProperties.getDatabase());
        }
        LettuceClientConfiguration lettuceClientConfiguration = null;
        if(null != pRedisProperties.getLettuce()){
            RedisProperties.Lettuce redisLettuce = pRedisProperties.getLettuce();
            if(null != redisLettuce.getPool() && null != redisLettuce.getPool().getEnabled() && redisLettuce.getPool().getEnabled()){
                RedisProperties.Pool pool = redisLettuce.getPool();
                GenericObjectPoolConfig<Object> poolConfig = new GenericObjectPoolConfig<>();
                poolConfig.setMinIdle(pool.getMinIdle());
                poolConfig.setMaxIdle(pool.getMaxIdle());
                poolConfig.setMaxTotal(pool.getMaxActive());
                lettuceClientConfiguration = LettucePoolingClientConfiguration.builder()
                        .poolConfig(poolConfig)
                        .commandTimeout(pRedisProperties.getTimeout())
                        .shutdownTimeout(redisLettuce.getShutdownTimeout())
                        .build();

            }else{
                LettuceClientConfiguration.LettuceClientConfigurationBuilder clientConfigBuilder = LettuceClientConfiguration.builder()
                        .commandTimeout(pRedisProperties.getTimeout())
                        .shutdownTimeout(redisLettuce.getShutdownTimeout());
                lettuceClientConfiguration = clientConfigBuilder.build();
            }
            LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisConfig, lettuceClientConfiguration);
            connectionFactory.afterPropertiesSet();
            redisConnectionFactory = connectionFactory;
        }else{
            LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisConfig);
            connectionFactory.afterPropertiesSet();
            redisConnectionFactory = connectionFactory;
        }
        return redisConnectionFactory;
    }
}
